package com.lsk.mps.backend.util;

import com.lsk.mps.backend.redis.RedisDao;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

public final class SecurityUtil {
    private static RedisDao redisDao = SpringUtil.getBean(RedisDao.class);

    public static Integer currentUID() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object principal = authentication.getPrincipal();
        if (principal instanceof String) {
            throw new StatusCode(403, "User not login");
        }
        String username = ((UserDetails) principal).getUsername();
        Integer id = Integer.parseInt(redisDao.get(username + "-ID"));
        return id;
    }
}
